#!/bin/sh
#
# hhvm        Starts The HHVM FastCGI Daemon
#
# chkconfig: - 85 15
# description: Starts The HHVM FastCGI Daemon
# processname: hhvm
# config: /etc/hhvm/server.ini
# config: /etc/sysconfig/hhvm
# pidfile: /var/run/hhvm/hhvm.pid
#
### BEGIN INIT INFO
# Provides: hhvm
# Short-Description: start and stop the HHVM FastCGI Daemon
# Description: Starts The HHVM FastCGI Daemon
### END INIT INFO

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
	exit 0
fi

RETVAL=0
HHVM=/usr/bin/hhvm
prog="hhvm"
pidfile=${PIDFILE-/var/run/hhvm/pid}
lockfile=${LOCKFILE-/var/run/hhvm/lock}

CONFIG_HDF_FILE="/etc/hhvm/config.hdf"
CONFIG_FILE="/etc/hhvm/server.ini"
SYSTEM_CONFIG_FILE="/etc/hhvm/php.ini"
MODE=server
USER=www
ARGS="--config ${CONFIG_FILE} --config ${SYSTEM_CONFIG_FILE} --config ${CONFIG_HDF_FILE}"

if [ -f /etc/sysconfig/hhvm ]; then
	. /etc/sysconfig/hhvm
fi


start() {
	echo -n $"Starting $prog: "
	dir=$(dirname ${pidfile})
	[ -d $dir ] || mkdir $dir
	daemon ${HHVM} --mode ${MODE} --user ${USER} ${ARGS}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch ${lockfile}
}


stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} ${HHVM}
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
		rm -f ${lockfile} ${pidfile}
	fi
}

restart () {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} ${HHVM}
        RETVAL=$?
        ;;
  restart)
	restart
        ;;
  *)
        echo $"Usage: $prog {start|stop|status|restart}"
        RETVAL=2
esac

exit $RETVAL
